<template>
  <div>
    <el-dialog title="更新权限" center width="500px" :visible.sync="isVisible" :close-on-press-escape="false" :close-on-click-modal="false">
      <el-form size="mini" status-icon label-width="80px" :rules="formRules" ref="form" v-if="rowData" :model="rowData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="rowData.name" autocomplete="off" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="权限URI" prop="uri">
          <el-input v-model="rowData.uri" autocomplete="off" placeholder="请输入权限URI" />
        </el-form-item>
        <el-form-item label="权限类型" prop="type">
          <el-radio-group v-model="rowData.type">
            <el-radio :label="0">目录</el-radio>
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="父级ID" prop="parentId">
          <el-select v-model="rowData.parentId" filterable placeholder="请选择父级ID">
            <el-option v-for="item in parentIdList" :key="item.id" :label="item.label" :value="item.id"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" :loading="loading" @click="updateHandle('form')">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'PermissionUpdate',
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      rowData: {
        type: Object,
        default: null
      }
    },
    computed: {
      isVisible: {
        get() {
          return this.visible
        },
        set(val) {
          this.$emit('update:visible', val)
        }
      }
    },
    watch: {
      isVisible(val) {
        if (val) {
          this.listPermissionNamesHandle()
        }
      }
    },
    data() {
      return {
        parentIdList: [],
        formRules: {
          name: [
            { required: true, message: '请输入权限角色', trigger: 'blur' }
          ],
          uri: [
            { required: true, message: '请输入权限URI', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请选择权限类型', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '请选择父级ID', trigger: 'blur' }
          ]
        },
        loading: false
      }
    },
    methods: {
      listPermissionNamesHandle() {
        this.AjaxUtils.get('/permission/listPermissionName').then((response) => {
          this.parentIdList = response.data
        }).catch(() => {
        })
      },
      /**
       * 更新权限
       * @param formName
       */
      updateHandle(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loading = true
            // params
            const params = new URLSearchParams()
            params.append('id', this.rowData.id)
            params.append('name', this.rowData.name)
            params.append('uri', this.rowData.uri)
            params.append('type', this.rowData.type)
            params.append('parentId', this.rowData.parentId)
            // ajax
            this.AjaxUtils.put('/permission/updatePermission', params, true).then((response) => {
              this.loading = false
              this.isVisible = false
              this.$emit('reloadTable')
              console.log(response)
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-select {
    display: block;
  }
</style>
